var filepicker=(function(){
    var a;
    var b=function(b){
        a=b;
    };   
    var c="https://www.filepicker.io";
    if(window.filepicker&&window.filepicker.hostname)c=window.filepicker.hostname;
    var d="v0";
    var e={
        ALL:'*/*',
        IMAGES:'image/*',
        JPG:'image/jpeg',
        GIF:'image/gif',
        PNG:'image/png',
        PDF:'application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf',
        AUDIO:'audio/*',
        MP3:'audio/mpeg',
        TEXT:'text/*',
        HTML:'text/html',
        XML:'text/xml',
        VCARD:'text/vcard',
        VIDEO:'video/*',
        MPEG:'video/mpeg',
        MP4:'video/mp4'
    };
    
    var f={
        BOX:0,
        COMPUTER:1,
        DROPBOX:2,
        FACEBOOK:3,
        GITHUB:4,
        GMAIL:5,
        IMAGE_SEARCH:6,
        URL:7,
        WEBCAM:8,
        GOOGLE_DRIVE:9,
        SEND_EMAIL:10,
        INSTAGRAM:11,
        FLICKR:12,
        VIDEO:13,
        EVERNOTE:14,
        PICASA:15,
        WEBDAV:16,
        FTP:17,
        ALFRESCO:18
    };
    
    var g=false;
    var h;
    var i=5;
    var j=1*1024*1024;
    var k=function(a){
        this.text=a;
        this.toString=function(){
            return "FilepickerException: "+this.text;
        };
    
};

var l=function(a){
    return Object.prototype.toString.call(a)==='[object Array]';
};

var m={
    OPEN:'/v0/dialog/open/',
    SAVEAS:'/v0/dialog/save/'
};

var n=/^(http|https)\:.*\/\//i;
var o="left=100,top=100,height=600,width=800,menubar=no,toolbar=no,location=no,personalbar=no,status=no,resizable=yes,scrollbars=yes,dependent=yes,dialog=yes";
var p="filepicker_dialog";
var q="filepicker_dialog_container";
var r="filepicker_shade";
var s="filepicker_iframe";
var t="filepicker_comm_iframe";
var u={};

u.tempStorage=c+"/v0/api/path/storage/";
u.commUrl=c+"/v0/dialog/comm_iframe/";
u.open=c+m.OPEN;
u.saveas=c+m.SAVEAS;
var v=function(b,c,e){
    return u.open+"?m="+encodeURIComponent(b.join(","))+"&key="+a+"&id="+c+"&referrer="+window.location.hostname+"&iframe="+(e.container!='window')+"&version="+d+(e.services?"&s="+e.services.join(","):"")+(e.multiple?"&multi="+e.multiple:"")+(e.location!==undefined?"&loc="+e.location:"")+(e.metadata?"&meta="+e.metadata:"")+(e.maxSize?"&maxSize="+e.maxSize:"")+(e.persist?"&p="+e.persist:"")+(e.auth_tokens?"&auth_tokens="+e.auth_tokens:"")+(e.conversions?"&conversions="+e.conversions:"");
};

var w=function(b,c,e,f){
    return u.saveas+"?url="+b+"&m="+encodeURIComponent(c)+"&key="+a+"&id="+e+"&referrer="+window.location.hostname+"&iframe="+(f.container!='window')+"&version="+d+(f.services?"&s="+f.services:"")+(f.defaultSaveasName?"&defaultSaveasName="+f.defaultSaveasName:"")+(f.location!==undefined?"&loc="+f.location:"");
};

var x=function(){
    if(window.frames[t]===undefined){
        y();
        var a;
        a=document.createElement("iframe");
        a.name=t;
        a.src=u.commUrl;
        a.style.display='none';
        document.body.appendChild(a);
    }
};

var y=function(){
    if(y.set)return;else y.set=true;
    var a=function(a){
        if(a.origin!=c)return;
        var b=JSON.parse(a.data);
        B.run(b);
    };
    
    if(window.addEventListener)window.addEventListener("message",a,false);
    else if(window.attachEvent)window.attachEvent("onmessage",a);else throw new k("Unsupported browser");
};

var z=function(a,b){
    var c=function(c){
        if(c.type!=="filepickerUrl")return;
        if(l(c.payload))a(c.payload);
        else if(b)a([c.payload]);else a(c.payload.url,c.payload.data);G();
        };
        return c;
        };
        var A=function(a){
        var b=function(b){
        if(b.type!=="ThirdPartyCookies")return;h=!!b.payload;if(a&&typeof a==="function")a(!!b.payload);
        };
        return b;
        };
        var B={};
        B._storage={
        'cookies':A()
        };
        B.attachHandler=function(a,b){
        B._storage[a]=b;return b;
        };
        B.detachHandler=function(a){
        return delete B._storage[a];
        };
        B.run=function(a){
        var b=a.id;if(B._storage.hasOwnProperty(b)){
        B._storage[b](a);return true;
        }
        return false;
        };
        var C=function(a){
        var b=D();var c=E();var d=F();var e=document.createElement("iframe");e.name=p;e.id=p;var f=N();var g=Math.min(f[1]-40,500);e.style.width='100%';e.style.height=g-10+'px';e.style.border="none";e.style.position="relative";e.setAttribute('frameborder',0);e.setAttribute('marginwidth',0);e.setAttribute('marginheight',0);e.src=a;document.body.appendChild(b);c.appendChild(d);c.appendChild(e);document.body.appendChild(c);return e;
        };
        var D=function(){
        var a=document.createElement("div");a.id=r;a.style.position='fixed';a.style.top=0;a.style.bottom=0;a.style.right=0;a.style.left=0;a.style.backgroundColor='#000000';a.style.opacity='0.5';a.style.filter='alpha(opacity=50)';a.style.zIndex=10000;a.onclick=filepicker.closeModal;return a;
        };
        var E=function(){
        var a=document.createElement("div");a.id=q;a.style.position='fixed';a.style.background='#ffffff url("https://www.filepicker.io/static/img/spinner.gif") no-repeat 50% 50%';a.style.top='10px';a.style.bottom='auto';a.style.right='auto';var b=N();var c=Math.min(b[1]-40,500);var d=Math.min(b[0]-40,800);var e=(b[0]-d-40)/2;a.style.left=e+"px";a.style.height=c+'px';a.style.width=d+'px';a.style.overflow='auto';a.style.webkitOverflowScrolling='touch';a.style.border='1px solid #999';a.style.webkitBorderRadius='3px';a.style.borderRadius='3px';a.style.margin='0';a.style.webkitBoxShadow='0 3px 7px rgba(0, 0, 0, 0.3)';a.style.boxShadow='0 3px 7px rgba(0, 0, 0, 0.3)';a.style.zIndex=10001;return a;
        };
        var F=function(){
        var a=document.createElement("a");a.appendChild(document.createTextNode('\u00D7'));a.onclick=filepicker.closeModal;a.style.position="absolute";a.style.zIndex=10;a.style.top="10px";a.style.right="15px";a.style.cursor="default";a.style.padding='0 5px 0 0px';a.style.fontSize='1.5em';a.style.color='#555555';a.style.textDecoration='none';return a;
        };
        var G=function(){
        var a=document.getElementById(r);if(a)document.body.removeChild(a);var b=document.getElementById(q);if(b)document.body.removeChild(b);
        };
        var H=function(){
        var a=new Date();return a.getTime().toString();
        };
        var I=function(a,b,c){
        a=R.normalizeMimetypes(a);return K(m.OPEN,{
            'mimetype':a,
    'options':b,
    'callback':c
            });
        };
        var J=function(a,b,c,d){
        return K(m.SAVEAS,{
            'file_url':a,
    'mimetype':b,
    'options':c,
    'callback':d
            });
        };
        var K=function(b,c){
        var d;var f;var i;if(!a)throw new k("API Key not found");if(b!=m.OPEN&&b!=m.SAVEAS)return null;if(b==m.SAVEAS){
        i=c.file_url;if(!i||typeof i!="string")throw new k("The provided File URL ('"+i+"') is not valid");if(!i.match(n))throw new k(i+" is not a valid url. Make sure it starts with http or https");i=encodeURIComponent(i);
        }
        var j=c.mimetype;var q=c.options;var r=c.callback;j=j||e.ALL;if(!l(j))j=[j];if(q===undefined)q={};else if(typeof q==="function"){
    r=q;
    q={};

}
r=r||function(){};

if(q.services&&!l(q.services))q.services=[q.services];
if(q.debug){
    dummy_url="https://www.filepicker.io/api/file/-nBq2onTSemLBxlcBWn1";
    data={
        'filename':'test.png',
        'type':'image/png',
        'size':58979
    };
    
    window.setTimeout(function(){
        if(q.multiple)r([{
            'url':dummy_url,
            'data':data
        },{
            'url':dummy_url,
            'data':data
        }]);else r(dummy_url,data);
    },100);
    return window;
}
if(q.container===undefined)if(q.modal===undefined)q.container=g?'window':'modal';else q.container=q.modal?'modal':'window';
if(q.container=='modal')if(R.isIOS()||R.isAndroid())q.container='window';
var s=q.container!='window';
var t=(N()[0]<768);
s=s&&!t;
if(h!==undefined){
    s=s&&h;
    x();
}else if(s){
    var u=function(){
        if(b==m.OPEN)I(j,q,r);
        else if(b==m.SAVEAS)J(i,j,q,r);
    };
    
    L(u);
    return null;
}
if(q.auth_tokens!==undefined){
    var y=JSON.stringify(q.auth_tokens);
    q.auth_tokens=encodeURIComponent(y);
}
if(q.conversions!==undefined){
    var A=JSON.stringify(q.conversions);
    q.conversions=encodeURIComponent(A);
}
var D=H();
var E;
if(b==m.OPEN)E=v(j,D,q);
else if(b==m.SAVEAS)E=w(i,j,D,q);
if(q.container=='window')d=window.open(E,p,o);
else if(q.container=='modal')d=C(E);
else{
    var F=document.getElementById(q.container);
    if(!F)throw new k("Container '"+q.container+"' not found. This should either be set to 'window','modal', or the ID of an iframe that is currently in the document.");
    F.src=E;
}
B.attachHandler(D,z(r,!!q.multiple));
return d;
};

var L=function(a){
    handler=A(a);
    B.attachHandler('cookies',handler);
    x();
};

var M={
    _keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode:function(a){
        var b="";
        var c,d,e,f,g,h,i;
        var j=0;
        a=M._utf8_encode(a);
        while(j<a.length){
            c=a.charCodeAt(j);
            d=a.charCodeAt(j+1);
            e=a.charCodeAt(j+2);
            j+=3;
            f=c>>2;
            g=((c&3)<<4)|(d>>4);
            h=((d&15)<<2)|(e>>6);
            i=e&63;
            if(isNaN(d))h=i=64;
            else if(isNaN(e))i=64;
            b=b+this._keyStr.charAt(f)+this._keyStr.charAt(g)+this._keyStr.charAt(h)+this._keyStr.charAt(i);
        }
        return b;
    },
    decode:function(a){
        var b="";
        var c,d,e;
        var f,g,h,i;
        var j=0;
        a=a.replace(/[^A-Za-z0-9\+\/\=]/g,"");
        while(j<a.length){
            f=this._keyStr.indexOf(a.charAt(j));
            g=this._keyStr.indexOf(a.charAt(j+1));
            h=this._keyStr.indexOf(a.charAt(j+2));
            i=this._keyStr.indexOf(a.charAt(j+3));
            j+=4;
            c=(f<<2)|(g>>4);
            d=((g&15)<<4)|(h>>2);
            e=((h&3)<<6)|i;
            b=b+String.fromCharCode(c);
            if(h!=64)b=b+String.fromCharCode(d);
            if(i!=64)b=b+String.fromCharCode(e);
        }
        b=M._utf8_decode(b);
        return b;
    },
    _utf8_encode:function(a){
        a=a.replace(/\r\n/g,"\n");
        var b="";
        for(var c=0;c<a.length;c++){
            var d=a.charCodeAt(c);
            if(d<128)b+=String.fromCharCode(d);
            else if((d>127)&&(d<2048)){
                b+=String.fromCharCode((d>>6)|192);
                b+=String.fromCharCode((d&63)|128);
            }else{
                b+=String.fromCharCode((d>>12)|224);
                b+=String.fromCharCode(((d>>6)&63)|128);
                b+=String.fromCharCode((d&63)|128);
            }
        }
        return b;
},
_utf8_decode:function(a){
    var b="";
    var c=0;
    var d=c1=c2=0;
    while(c<a.length){
        d=a.charCodeAt(c);
        if(d<128){
            b+=String.fromCharCode(d);
            c++;
        }else if((d>191)&&(d<224)){
            c2=a.charCodeAt(c+1);
            b+=String.fromCharCode(((d&31)<<6)|(c2&63));
            c+=2;
        }else{
            c2=a.charCodeAt(c+1);
            c3=a.charCodeAt(c+2);
            b+=String.fromCharCode(((d&15)<<12)|((c2&63)<<6)|(c3&63));
            c+=3;
        }
    }
    return b;
}
};

var N=function(){
    if(document.body&&document.body.offsetWidth){
        winW=document.body.offsetWidth;
        winH=document.body.offsetHeight;
    }
    if(document.compatMode=='CSS1Compat'&&document.documentElement&&document.documentElement.offsetWidth){
        winW=document.documentElement.offsetWidth;
        winH=document.documentElement.offsetHeight;
    }
    if(window.innerWidth&&window.innerHeight){
        winW=window.innerWidth;
        winH=window.innerHeight;
    }
    return [winW,winH];
};

var O=function(b,c,d,e){
    if(typeof c==="function"){
        e=!!d;
        d=c;
        c={};
    
}else e=!!e;
if(!c)c={};
    
if(!c.filename)c.filename='';
d=d||function(){};

if(!b)throw new k('Error: no contents given');
var f;
b=e?b:M.encode(b);
var g=R.ajax({
    method:'POST',
    url:u.tempStorage+c.filename,
    data:{
        fileContents:b,
        apikey:a,
        _cachebust:new Date().valueOf()
        },
    json:true,
    success:function(a){
        if(a.result=="ok"){
            f=a.data;
            d(f.url,f.data);
        }else d(null,a);
    },
    error:function(a){
        d(null);
    }
});
};

var P=function(a,b,c){
    if(typeof b==="function"){
        c=b;
        b=false;
    }
    b=!!b;
    R.ajax({
        method:'GET',
        url:a,
        data:{
            'base64encode':b
        },
        headers:{
            'X-NO-STREAM':true
        },
        success:function(a){
            c(a);
        }
    });
};

var Q=function(b,c){
    if(!a)throw new k("API Key not found");
    b+='/revoke';
    var d=R.ajax({
        method:'POST',
        url:b,
        success:function(a){
            c(true,"success");
        },
        error:function(a){
            c(false,a);
        },
        data:{
            "key":a
        }
    });
};

var R={};

R.addOnLoad=function(a){
    if(window.jQuery)window.jQuery(function(){
        a();
    });
    else{
        var b="load";
        if(window.addEventListener)window.addEventListener(b,a,false);
        else if(window.attachEvent)window.attachEvent("on"+b,a);
        else if(window.onload){
            var c=window.onload;
            window.onload=function(){
                c();
                a();
            };
        
    }else window.onload=a;
}
};

R.typeOf=function(a){
    if(a===null)return 'null';
    else if(Object.prototype.toString.apply(a)==='[object Array]')return 'array';
    return typeof a;
};

R.JSON=(function(){
    if(typeof JSON=='undefined')this.JSON={};
        
    var a={
        '\b':'\\b',
        '\t':'\\t',
        '\n':'\\n',
        '\f':'\\f',
        '\r':'\\r',
        '"':'\\"',
        '\\':'\\\\'
    };
    
    var b=function(b){
        return a[b]||'\\u'+('0000'+b.charCodeAt(0).toString(16)).slice(-4);
    };
    
    var c=function(a){
        a=a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,'');
        return(/^[\],:{}\s]*$/).test(a);
    };
    
    var d=JSON.stringify?function(a){
        return JSON.stringify(a);
    }:function(a){
        if(a&&a.toJSON)a=a.toJSON();
        switch(R.typeOf(a)){
            case 'string':
                return '"'+a.replace(/[\x00-\x1f\\"]/g,b)+'"';
            case 'array':
                return '['+a.map(d).clean()+']';
            case 'object':case 'hash':
                var c=[];
                Object.each(a,function(a,b){
                var e=d(a);
                if(e)c.push(d(b)+':'+e);
            });
            return '{'+c+'}';
            case 'number':case 'boolean':
                return ''+a;
            case 'null':
                return 'null';
            default:
                return 'null';
        }
        return null;
    };
    
    var e=function(a,b){
        if(!a||R.typeOf(a)!='string')return null;
        if(JSON.parse)return JSON.parse(a);
        else{
            if(b)if(!c(a))throw new Error('JSON could not decode the input; security is enabled and the value is not secure.');
            return eval('('+a+')');
        }
    };
    
return{
    validate:c,
    encode:d,
    decode:e
};

})();
R.isIOS=function(){
    if(navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/iPad/i))return true;else return false;
};

R.isAndroid=function(){
    if(navigator.userAgent.match(/Android/i))return true;else return false;
};

R.ajax=(function(){
    var a=function(b,c){
        var d=[];
        for(var e in b){
            var f=b[e];
            if(c)e=c+'['+e+']';
            var g;
            switch(R.typeOf(f)){
                case 'object':
                    g=a(f,e);
                    break;
                case 'array':
                    var h={};
                    
                    f.each(function(a,b){
                    h[b]=a;
                });
                g=a(h,e);
                    break;
                default:
                    g=e+'='+encodeURIComponent(f);
                    break;
            }
            if(f!==null)d.push(g);
        }
        return d.join('&');
    };
    
    var b=function(b){
        var d=b.url||null;
        var e=b.method?b.method.toUpperCase():"POST";
        var f=b.success||function(){};
        
        var g=b.error||function(){};
        
        var h=b.async===undefined?true:b.async;
        var i=b.data||null;
        var j=b.processData===undefined?true:b.processData;
        var k=b.headers||{};
        
        if(i&&j)i=a(b.data);
        if(window.XDomainRequest)return c(b);
        var l;
        if(b.xhr)l=b.xhr;else try{
            l=new XMLHttpRequest();
        }catch(m){
            try{
                l=new ActiveXObject("Msxml2.XMLHTTP");
            }catch(m){
                try{
                    l=new ActiveXObject("Microsoft.XMLHTTP");
                }catch(m){
                    throw "Your browser does not support AJAX and so cannot excecute this method";
                }
            }
        }
        var n=function(){
    if(l.readyState==4)if(l.status>=200&&l.status<300){
        var a=l.responseText;
        if(b.json)a=R.JSON.decode(a);
        f(a,l);
    }else g(l.responseText,l);
    l.onreadystatechage=function(){};

};

l.onreadystatechange=n;
if(i&&e=='GET'){
    d+=(d.indexOf('?')!=-1?'&':'?')+i;
    i=null;
}
l.open(e,d,h);
if(b.json)l.setRequestHeader('Accept','application/json');else l.setRequestHeader('Accept','text/javascript, text/html, application/xml, text/xml, */*');
if(i&&j&&(e=="POST"||e=="PUT"))l.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
if(k)for(var o in k)l.setRequestHeader(o,k[o]);l.send(i);
return l;
};

var c=function(b){
    if(!window.XDomainRequest)return null;
    var c=b.url||null;
    var d=b.method?b.method.toUpperCase():"POST";
    var e=b.success||function(){};
    
    var f=b.error||function(){};
    
    var g=b.data||{};
    
    if(window.location.protocol=="http:")c=c.replace("https:","http:");
    else if(window.location.protocol=="https:")c=c.replace("http:","https:");
    if(b.async)throw new k("Asyncronous Cross-domain requests are not supported");
    if(d!="GET"&&d!="POST"){
        g._method=d;
        d="POST";
    }
    g=g?a(g):null;
    if(g&&d=='GET'){
        c+=(c.indexOf('?')>=0?'&':'?')+g;
        g=null;
    }
    var h=new window.XDomainRequest();
    h.onload=function(){
        var a=h.responseText;
        if(b.json)a=R.JSON.decode(a);
        e(a,h);
    };
    
    h.onerror=function(){
        var a=h.responseText||'error';
        f(a,h);
    };
    
    h.open(d,c,true);
    h.send(g);
    return h;
};

return b;
})();
R.trim=function(a){
    return a.replace(/^\s+|\s+$/g,"");
};

R.normalizeMimetypes=function(a){
    if(R.typeOf(a)=="string")return R.normalizeMimetypeString(a);
    else if(R.typeOf(a)=="array"){
        var b=[];
        for(var c=0;c<a.length;c++){
            partial=R.normalizeMimetypes(a[c]);
            b=b.concat(partial);
        }
        return b;
    }else return ['*/*'];
};

R.normalizeMimetypeString=function(a){
    out=a.split(',');
    for(var b=0;b<out.length;b++)out[b]=R.trim(out[b]);
    return out;
};

R.mimetype_extension_map={
    '.stl':'applicaiton/sla',
    '.hbs':'text/html',
    '.pdf':'application/pdf',
    '.jpg':'image/jpeg',
    '.jpeg':'image/jpeg',
    '.jpe':'image/jpeg',
    '.imp':'application/x-impressionist'
};

R.mimetype_bad_array=['application/octet-stream','application/download','application/force-download','octet/stream','application/unknown'];
R.getMimetype=function(a){
    if(a.type){
        var b=a.type;
        b=b.toLowerCase();
        var c=false;
        for(var d=0;d<R.mimetype_bad_array.length;d++)c=c||b==R.mimetype_bad_array[d];
        if(!c)return a.type;
    }
    var e=a.name||a.fileName;
    var f=e.match(/\.\w*$/);
    if(f)return R.mimetype_extension_map[f[0].toLowerCase()]||'';
    else if(a.type)return a.type;else return '';
};

R.matchesMimetype=function(a,b){
    if(!a)return b=="*/*";
    a=R.trim(a);
    b=R.trim(b);
    test_parts=a.split("/");
    against_parts=b.split("/");
    if(against_parts[0]=="*")return true;
    if(against_parts[0]!=test_parts[0])return false;
    if(against_parts[1]=="*")return true;
    return against_parts[1]==test_parts[1];
};

R.addOnLoad(function(a){
    L();
});
var S=function(){
    var a=document.querySelectorAll('input[type="filepicker"]');
    for(var b=0;b<a.length;b++)U(a[b]);
};

var T=function(a){
    var b=a.getAttribute('type');
    if(b=='filepicker')U(a);
    else if(b=='filepicker-dragdrop')Y(a);else W(a);
};

var U=function(a){
    var c;
    var d;
    var g;
    var h;
    var i;
    var j;
    var k;
    var l;
    d=document.createElement("button");
    d.innerHTML=a.getAttribute('data-fp-button-text')||a.getAttribute('data-fp-text')||"Pick File";
    d.className=a.getAttribute('data-fp-button-class')||a.getAttribute('data-fp-class')||a.className;
    a.style.display="none";
    h=(a.getAttribute("data-fp-mimetypes")||e.ALL).split(",");
    i={};
    
    l=a.getAttribute("data-fp-option-container");
    if(l)i.container=l;
    else{
        modal=(a.getAttribute("data-fp-option-modal")||"true")!="false";
        i.container=modal?'modal':'window';
    }
    i.multiple=(a.getAttribute("data-fp-option-multiple")||"false")!="false";
    i.persist=(a.getAttribute("data-fp-option-persist")||"false")!="false";
    if(a.getAttribute("data-fp-option-maxsize"))i.maxSize=a.getAttribute("data-fp-option-maxsize");
    k=a.getAttribute("data-fp-option-services");
    if(k){
        k=k.split(",");
        for(c=0;c<k.length;c++)k[c]=f[k[c].replace(" ","")];
        i.services=k;
    }
    loc=a.getAttribute("data-fp-option-location");
    if(loc)i.location=loc;
    j=a.getAttribute("data-fp-apikey");
    if(j)b(j);
    d.onclick=(function(a,b,e){
        return function(){
            d.blur();
            I(b,e,function(b,d){
                var f;
                if(e.multiple){
                    var g=[];
                    for(c=0;c<b.length;c++)g.push(b[c].url);
                    a.value=g.join();
                    ad(a,b);
                }else{
                    a.value=b;
                    ad(a,[{
                        url:b,
                        data:d
                    }]);
                }
            });
        return false;
    };
    
})(a,h,i);
a.parentNode.insertBefore(d,a);
};

var V=function(){
    var a=[];
    var b=document.querySelectorAll('button[data-fp-url]');
    var c;
    for(c=0;c<b.length;c++)a.push(b[c]);
    b=document.querySelectorAll('a[data-fp-url]');
    for(c=0;c<b.length;c++)a.push(b[c]);
    b=document.querySelectorAll('input[type="button"][data-fp-url]');
    for(c=0;c<b.length;c++)a.push(b[c]);
    for(c=0;c<a.length;c++)W(a[c]);
};

var W=function(a){
    a.onclick=(function(a){
        return function(){
            var c=a.getAttribute("data-fp-mimetype");
            var d=a.getAttribute("data-fp-url");
            if(!c||!d)return true;
            var e={};
            
            var g=a.getAttribute("data-fp-option-container");
            if(g)e.container=g;
            else{
                modal=(a.getAttribute("data-fp-option-modal")||"true")!="false";
                e.container=modal?'modal':'window';
            }
            var h=a.getAttribute("data-fp-option-services");
            if(h){
                h=h.split(",");
                for(j=0;j<h.length;j++)h[j]=f[h[j].replace(" ","")];
                e.services=h;
            }
            var i=a.getAttribute("data-fp-option-defaultSaveasName");
            if(i)e.defaultSaveasName=i;
            apikey=a.getAttribute("data-fp-apikey");
            if(apikey)b(apikey);
            J(d,c,e);
            return false;
        };
    
    })(a);
};

var X=function(){
    var a=document.querySelectorAll('input[type="filepicker-dragdrop"]');
    for(var b=0;b<a.length;b++)Y(a[b]);
};

var Y=function(a){
    var c;
    var d;
    var g;
    var h;
    var i;
    var j;
    var k;
    var l;
    var m;
    d=document.createElement("div");
    d.className=a.getAttribute('data-fp-class')||a.className;
    d.style.padding="1px";
    d.style.display="inline-block";
    a.style.display="none";
    a.parentNode.insertBefore(d,a);
    g=document.createElement("button");
    g.innerHTML=a.getAttribute('data-fp-button-text')||"Pick File";
    g.className=a.getAttribute('data-fp-button-class')||'';
    d.appendChild(g);
    var n=document.createElement("div");
    $(n);
    var o=d.offsetWidth-g.offsetWidth-20;
    o=Math.min(o,220);
    if(o>0)n.style.width=o+"px";
    n.innerHTML=a.getAttribute('data-fp-drag-text')||"Or drop files here";
    n.className=a.getAttribute('data-fp-drag-class')||'';
    d.appendChild(n);
    i=(a.getAttribute("data-fp-mimetypes")||e.ALL).split(",");
    j={};
    
    m=a.getAttribute("data-fp-option-container");
    if(m)j.container=m;
    else{
        modal=(a.getAttribute("data-fp-option-modal")||"true")!="false";
        j.container=modal?'modal':'window';
    }
    j.multiple=(a.getAttribute("data-fp-option-multiple")||"false")!="false";
    j.persist=(a.getAttribute("data-fp-option-persist")||"false")!="false";
    j.mimetypes=i;
    if(a.getAttribute("data-fp-option-maxsize"))j.maxSize=a.getAttribute("data-fp-option-maxsize");
    l=a.getAttribute("data-fp-option-services");
    if(l){
        l=l.split(",");
        for(c=0;c<l.length;c++)l[c]=f[l[c].replace(" ","")];
        j.services=l;
    }
    k=a.getAttribute("data-fp-apikey");
    if(k)b(k);
    var p=(!!window.FileReader||navigator.userAgent.indexOf("Safari")>=0)&&('draggable' in n);
    if(p)_(n,j,a);else n.innerHTML="&nbsp;";
    n.onclick=g.onclick=(function(a,b,d,e){
        return function(){
            I(d,e,function(d,f){
                var g;
                if(e.multiple){
                    var h=[];
                    var i=[];
                    for(c=0;c<d.length;c++){
                        h.push(d[c].url);
                        i.push(d[c].data.filename);
                    }
                    a.value=h.join();
                    Z(a,b,i.join(', '));
                    ad(a,d);
                }else{
                    a.value=d;
                    Z(a,b,f.filename);
                    ad(a,[{
                        url:d,
                        data:f
                    }]);
                }
            });
        return false;
    };
    
})(a,n,i,j);
};

var Z=function(a,b,c){
    b.innerHTML=c;
    b.style.width=Math.max(0,b.offsetWidth-10)+"px";
    b.style.padding="2px 4px";
    b.style.cursor="default";
    var d=document.createElement("span");
    d.innerHTML="X";
    d.style.borderRadius="8px";
    d.style.fontSize="14px";
    d.style.cssFloat="right";
    d.style.padding="0 3px";
    d.style.color="#600";
    d.style.cursor="pointer";
    var e=function(c){
        if(!c)c=window.event;
        c.cancelBubble=true;
        if(c.stopPropagation)c.stopPropagation();
        $(b);
        var d=(!!window.FileReader||navigator.userAgent.indexOf("Safari")>=0)&&('draggable' in b);
        if(!d)b.innerHTML='&nbsp;';else b.innerHTML=a.getAttribute('data-fp-drag-text')||"Or drop files here";
        a.value='';
        ad(a);
        return false;
    };
    
    if(d.addEventListener)d.addEventListener("click",e,false);
    else if(d.attachEvent)d.attachEvent("onclick",e);
    b.appendChild(d);
};

var $=function(a){
    a.style.border="1px dashed #AAA";
    a.style.display="inline-block";
    a.style.margin="0 0 0 4px";
    a.style.borderRadius="3px";
    a.style.backgroundColor="#F3F3F3";
    a.style.color="#333";
    a.style.fontSize="14px";
    a.style.lineHeight="22px";
    a.style.padding="2px 4px";
    a.style.verticalAlign="middle";
    a.style.cursor="pointer";
    a.style.overflow="hidden";
};

var _=function(a,b,c){
    var d=a.innerHTML;
    var e;
    ak(a,{
        multiple:b.multiple,
        persist:b.persist,
        maxSize:b.maxSize,
        mimetypes:b.mimetypes,
        dragEnter:function(){
            a.innerHTML="Drop to upload";
            a.style.backgroundColor="#E0E0E0";
            a.style.border="1px solid #000";
        },
        dragLeave:function(){
            a.innerHTML=d;
            a.style.backgroundColor="#F3F3F3";
            a.style.border="1px dashed #AAA";
        },
        error:function(b,c){
            if(b=="TooManyFiles")a.innerHTML=c;
            else if(b=="WrongType")a.innerHTML=c;
            else if(b=="NoFilesFound")a.innerHTML=c;
            else if(b=="UploadError")a.innerHTML="Oops! Had trouble uploading.";
        },
        begin:function(b){
            e=ac(a);
        },
        progress:function(a){
            if(e)e.style.width=a+"%";
        },
        done:function(b){
            var d=[];
            var e=[];
            for(var f=0;f<b.length;f++){
                var g=b[f];
                d.push(g.url);
                e.push(g.data.filename);
            }
            c.value=d.join();
            Z(c,a,e.join(', '));
            ad(c,b);
        }
    });
};

var ab=function(b,c,d,e,f){
    f=f||{};
    
    var g=new XMLHttpRequest();
    g.upload.addEventListener("progress",e,false);
    var h=b.name||b.fileName;
    var i=u.tempStorage+h+"?apikey="+a+"&_cachebust="+new Date().valueOf();
    data=new FormData();
    data.append('fileUpload',b);
    R.ajax({
        xhr:g,
        method:'POST',
        url:i,
        json:true,
        success:c,
        error:d,
        processData:false,
        data:data
    });
};

var ac=function(a){
    var b=document.createElement("div");
    var c=a.offsetHeight-2;
    b.style.height=c+"px";
    b.style.backgroundColor="#0E90D2";
    b.style.width="2%";
    b.style.borderRadius="3px";
    a.style.padding="0";
    a.style.width=(a.offsetWidth+6)+"px";
    a.style.border="1px solid #AAA";
    a.style.backgroundColor="#F3F3F3";
    a.style.boxShadow="inset 0 1px 2px rgba(0, 0, 0, 0.1)";
    a.innerHTML="";
    a.appendChild(b);
    return b;
};

var ad=function(a,b){
    var c;
    if(document.createEvent){
        c=document.createEvent('Event');
        c.initEvent("change",true,false);
        c.files=b;
        a.dispatchEvent(c);
    }else if(document.createEventObject){
        c=document.createEventObject('Event');
        c.eventPhase=2;
        c.currentTarget=c.srcElement=c.target=a;
        c.files=b;
        a.fireEvent('onchange',c);
    }else if(a.onchange)a.onchange(b);
};

var ae=function(b,d,e,f,g){
    var h={};
    
    h.startMultipart=c+"/v0/api/upload/multipart/start/";
    h.sendMultipart=c+"/v0/api/upload/multipart/upload/";
    h.finishMultipart=c+"/v0/api/upload/multipart/end/";
    g=g||{};
    
    g.persist=!!g.persist;
    var k=0;
    var l=j;
    var m=Math.ceil(b.size/l);
    var n={
        'name':b.name||b.fileName,
        'size':b.size
        };
        
    R.ajax({
        method:"POST",
        url:h.startMultipart,
        json:true,
        data:{
            "apikey":a,
            "persist":g.persist,
            "name":b.name||b.fileName,
            "size":b.size
            },
        success:function(c){
            if(c.result!="ok"){
                e(c);
                return;
            }
            var g=c.data.id;
            if(!g)if(window.console&&window.console.error)window.console.error("Couldn't find multipart id");
            var j=function(b){
                k++;
                f({
                    loaded:k,
                    total:m,
                    lengthComputable:true
                });
                if(k===m)R.ajax({
                    method:"POST",
                    url:h.finishMultipart,
                    data:{
                        'id':g,
                        'total':m,
                        'apikey':a
                    },
                    success:d,
                    json:true
                });
            };
            
            var o={};
            
            var p=function(a,b){
                return function(c){
                    if(!o[a])o[a]=0;
                    o[a]++;
                    if(o[a]>i)e.call(this);else q(a,b);
                };
            
        };
        
        var q=function(b,c){
            var d=h.sendMultipart+"?id="+g+"&index="+b+"&apikey="+a;
            n=new FormData();
            n.append('fileUpload',c);
            R.ajax({
                method:'POST',
                url:d,
                json:true,
                success:j,
                error:p(b,c),
                processData:false,
                data:n
            });
        };
        
        var r=function(a,b){
            q(a,b);
        };
        
        var s;
        if(b.slice){
            var t=/(mozilla)(?:.*? rv:([\w.]+))?/;
            var u=t.exec(navigator.userAgent);
            if(u&&u[2]&&parseInt(u[2],10)<13)for(s=0;s<m;s++)r(s,b.slice(s*l,l));else for(s=0;s<m;s++)r(s,b.slice(s*l,(s+1)*l));
        }else if(b.mozSlice)for(s=0;s<m;s++)r(s,b.mozSlice(s*l,(s+1)*l));
        else if(b.webkitSlice)for(s=0;s<m;s++)r(s,b.webkitSlice(s*l,(s+1)*l));
        else{
            m=1;
            r(0,b);
        }
    }
    });
};

var af=function(a,b,c,d,e,f){
    e=e||i;
    b=b||function(){};
    
    c=c||function(){};
    
    d=d||function(){};
    
    if(!a.value)throw new k("File input is empty");
    var g=!!window.FormData&&!!window.XMLHttpRequest;
    if(g)ag(a,b,c,d,e,f);else ai(a,b,c,f);
};

var ag=function(a,b,c,d,e,f){
    if(a.files.length==1)ah(a.files[0],b,c,d,e);
    else{
        var g=0;
        var h=a.files.length;
        var i=[];
        var j=false;
        var k={};
        
        var l=function(){
            var a=0;
            for(index in k)a+=(k[index]||0);d(a/h);
        };
        
        var m=function(a){
            return function(b){
                k[a]=b;
                l();
            };
        
    };
    
    var n=function(a){
        if(j)return;
        i.push(a);
        g++;
        if(g==h){
            j=true;
            b(i);
        }
    };
    
var o=function(a){
    j=true;
    c(a);
};

for(var p=0;p<a.files.length;p++)ah(a.files[p],n,o,m(p),e,f);
}
};

var ah=function(a,b,c,d,e,f){
    var g=function(a){
        if(a.lengthComputable)d(Math.round((a.loaded*95)/a.total));
    };
    
    var h=0;
    var i=function(b){
        h++;
        if(h>e)c(b);else ab(a,j,i,g,f);
    };
    
    var j=function(a){
        if(a.result=='error'){
            i(a.error.msg);
            return;
        }
        d(100);
        b(a.data[0]);
    };
    
    ab(a,j,i,g,f);
};

var ai=function(b,c,d,e){
    e=e||{};
    
    x();
    var f="upload_iframe";
    var g;
    g=document.createElement("iframe");
    g.id=f;
    g.name=f;
    g.style.display='none';
    document.body.appendChild(g);
    B.attachHandler('upload',aj(c,d));
    var h=document.createElement("form");
    h.method="POST";
    h.action=u.tempStorage+"?format=iframe&apikey="+a+"&_cachebust="+new Date().valueOf();
    h.encoding=h.enctype="multipart/form-data";
    h.target=f;
    document.body.appendChild(h);
    var i=b.parentNode;
    var j=b.nextSibling;
    var k=b.name;
    b.name="fileUpload";
    h.appendChild(b);
    window.setTimeout(function(){
        h.submit();
        i.insertBefore(b,j);
        b.name=k;
        b.focus();
    },1);
};

var aj=function(a,b){
    var c=function(c){
        if(c.type!=="Upload")return;
        var d=c.payload;
        if(d.result=='ok'){
            if(a&&typeof a==="function")a(d.data);
        }else if(b&&typeof b==="function")b(d.msg);
        B.detachHandler("Upload");
    };
    
    return c;
};

var ak=function(a,b){
    if(!a)throw new k("No DOM element found to create drop pane");
    if(a.jquery){
        if(a.length===0)throw new k("No DOM element found to create drop pane");
        a=a[0];
    }
    var c=(navigator.vendor&&navigator.vendor.indexOf('Apple')>=0);
    var d=(!!window.FileReader||c)&&('draggable' in document.createElement('span'));
    if(!d){
        if(window.console&&window.console.error)window.console.error("Your browser doesn't support drag-drop functionality");
        return false;
    }
    b=b||{};
    
    var e=b.dragEnter||function(){};
    
    var f=b.dragLeave||function(){};
    
    var g=b.begin||function(){};
    
    var h=b.done||function(){};
    
    var i=b.error||function(){};
    
    var j=b.progress||function(){};
    
    var l=b.mimetypes||'*/*';
    if(R.typeOf(l)=='string')l=l.split(',');
    var m={
        persist:!!b.persist
        };
        
    a.addEventListener("dragenter",function(a){
        e();
        a.stopPropagation();
        a.preventDefault();
        return false;
    },false);
    a.addEventListener("dragleave",function(a){
        f();
        a.stopPropagation();
        a.preventDefault();
        return false;
    },false);
    a.addEventListener("dragover",function(a){
        a.preventDefault();
        return false;
    },false);
    a.addEventListener("drop",function(a){
        a.stopPropagation();
        a.preventDefault();
        var b=a.dataTransfer.files;
        if(n(b)){
            g(b);
            al(b,h,i,j,m);
        }
    });
var n=function(a){
    if(a.length>0){
        if(a.length>1&&!b.multiple){
            i("TooManyFiles","Only one file at a time");
            return false;
        }
        var c;
        var d;
        for(var e=0;e<a.length;e++){
            c=false;
            d=a[e];
            for(var f=0;f<l.length;f++){
                var g=R.getMimetype(d);
                c=c||R.matchesMimetype(g,l[f]);
            }
            if(!c){
                i("WrongType",(d.name||d.fileName)+" isn't the right type of file");
                break;
            }
        }
        if(c)return true;
}else i("NoFilesFound","No files uploaded");
return false;
};

return true;
};

var al=function(a,b,c,d,e){
    var f={};
    
    var g=0;
    var h=[];
    var i=function(){
        var b=0;
        var c;
        for(c in f)b+=f[c];percentage=b/a.length;
        d(percentage);
    };
    
    var j=function(a){
        return function(b){
            f[a]=b;
            i();
        };
    
};

var k=function(c){
    return function(d){
        d.data.filename=a[c].name||a[c].fileName||d.data.filename;
        d.data.size=a[c].size||d.data.size;
        d.data.type=a[c].type||d.data.type;
        h=h||[];
        h.push(d);
        g++;
        if(g>=a.length)b(h);
    };

};

for(var l=0;l<a.length;l++)am(a[l],k(l),c,j(l),e);
};

var am=function(a,b,c,d,e){
    var f=function(a){
        if(a.lengthComputable)d(Math.round((a.loaded*95)/a.total));
    };
    
    var g=a.name||a.fileName;
    var h=0;
    var k=function(b){
        h++;
        if(h>i)c('UploadError');else ab(a,m,k,f,e);
    };
    
    var m=function(a){
        if(a.result=='ok'){
            d(100);
            var c=a.data;
            if(!l(c)||c.length===0)k("No files found");
            b(a.data[0]);
        }else k(a.error.msg);
    };
    
    var n=!!(a.mozSlice||a.webkitSlice||a.slice);
    if(a.size>j&&n)ae(a,m,k,f,e);else ab(a,m,k,f,e);
};

var an=function(a,b,c){
    if(a[b]===undefined)return c;
    return a[b];
};

if(document.querySelectorAll){
    var ao=function(){
        S();
        V();
        X();
    };
    
    R.addOnLoad(ao);
}
return{
    closeModal:G,
    uploadFile:af,
    makeDropPane:ak,
    getFile:I,
    saveAs:J,
    MIMETYPES:e,
    SERVICES:f,
    setKey:b,
    getUrlFromData:O,
    revokeFile:Q,
    getContents:P,
    constructOpenWidget:U,
    constructSaveWidget:W,
    constructDragWidget:Y,
    constructWidget:T,
    _oci:x
};

})();